<?php
define("EW_PAGE_ID", "add", TRUE); // Page ID
define("EW_TABLE_NAME", 'chating', TRUE);
?>
<?php 
session_start(); // Initialize session data
ob_start(); // Turn on output buffering
?>
<?php include "ewcfg50.php" ?>
<?php include "ewmysql50.php" ?>
<?php include "phpfn50.php" ?>
<?php include "chatinginfo.php" ?>
<?php include "userfn50.php" ?>
<?php include "usersinfo.php" ?>
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Always modified
header("Cache-Control: private, no-store, no-cache, must-revalidate"); // HTTP/1.1 
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
?>
<?php

// Open connection to the database
$conn = ew_Connect();
?>
<?php
$Security = new cAdvancedSecurity();
?>
<?php
if (!$Security->IsLoggedIn()) $Security->AutoLogin();
$Security->LoadCurrentUserLevel('chating');
if (!$Security->IsLoggedIn()) {
	$Security->SaveLastUrl();
	Page_Terminate("login.php");
}
if (!$Security->CanAdd()) {
	$Security->SaveLastUrl();
	Page_Terminate("chatinglist.php");
}
?>
<?php

// Common page loading event (in userfn*.php)
Page_Loading();
?>
<?php

// Page load event, used in current page
Page_Load();
?>
<?php
$chating->Export = @$_GET["export"]; // Get export parameter
$sExport = $chating->Export; // Get export parameter, used in header
$sExportFile = $chating->TableVar; // Get export file, used in header
?>
<?php

// Load key values from QueryString
$bCopy = TRUE;
if (@$_GET["no"] != "") {
  $chating->no->setQueryStringValue($_GET["no"]);
} else {
  $bCopy = FALSE;
}

// Create form object
$objForm = new cFormObj();

// Process form if post back
if (@$_POST["a_add"] <> "") {
  $chating->CurrentAction = $_POST["a_add"]; // Get form action
  LoadFormValues(); // Load form values
} else { // Not post back
  if ($bCopy) {
    $chating->CurrentAction = "C"; // Copy Record
  } else {
    $chating->CurrentAction = "I"; // Display Blank Record
    LoadDefaultValues(); // Load default values
  }
}

// Perform action based on action code
switch ($chating->CurrentAction) {
  case "I": // Blank record, no action required
		break;
  case "C": // Copy an existing record
   if (!LoadRow()) { // Load record based on key
      $_SESSION[EW_SESSION_MESSAGE] = "No records found"; // No record found
      Page_Terminate($chating->getReturnUrl()); // Clean up and return
    }
		break;
  case "A": // ' Add new record
		$chating->SendEmail = TRUE; // Send email on add success
    if (AddRow()) { // Add successful
      $_SESSION[EW_SESSION_MESSAGE] = "Add New Record Successful"; // Set up success message
      Page_Terminate($chating->KeyUrl($chating->getReturnUrl())); // Clean up and return
    } else {
      RestoreFormValues(); // Add failed, restore form values
    }
}

// Render row based on row type
$chating->RowType = EW_ROWTYPE_ADD;  // Render add type
RenderRow();
?>
<?php include "header.php" ?>
<script type="text/javascript">
<!--
var EW_PAGE_ID = "add"; // Page id

//-->
</script>
<script type="text/javascript">
<!--

function ew_ValidateForm(fobj) {
	if (fobj.a_confirm && fobj.a_confirm.value == "F")
		return true;
	var i, elm, aelm, infix;
	var rowcnt = (fobj.key_count) ? Number(fobj.key_count.value) : 1;
	for (i=0; i<rowcnt; i++) {
		infix = (fobj.key_count) ? String(i+1) : "";
		elm = fobj.elements["x" + infix + "_yahoo_name"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - Name"))
				return false;
		}
	}
	return true;
}

//-->
</script>
<script type="text/javascript">
<!--
var ew_DHTMLEditors = [];

//-->
</script>
<script type="text/javascript">
<!--
var ew_MultiPagePage = "Page"; // multi-page Page Text
var ew_MultiPageOf = "of"; // multi-page Of Text
var ew_MultiPagePrev = "Prev"; // multi-page Prev Text
var ew_MultiPageNext = "Next"; // multi-page Next Text

//-->
</script>
<script language="JavaScript" type="text/javascript">
<!--

// Write your client script here, no need to add script tags.
// To include another .js script, use:
// ew_ClientScriptInclude("my_javascript.js"); 
//-->

</script>
<p><span class="phpmaker">Add to TABLE: chating<br><br><a href="<?php echo $chating->getReturnUrl() ?>">Go Back</a></span></p>
<?php
if (@$_SESSION[EW_SESSION_MESSAGE] <> "") { // Mesasge in Session, display
?>
<p><span class="ewmsg"><?php echo $_SESSION[EW_SESSION_MESSAGE] ?></span></p>
<?php
  $_SESSION[EW_SESSION_MESSAGE] = ""; // Clear message in Session
}
?>
<form name="fchatingadd" id="fchatingadd" action="chatingadd.php" method="post" onSubmit="return ew_ValidateForm(this);">
<p>
<input type="hidden" name="a_add" id="a_add" value="A">
<table class="ewTable">
  <tr class="ewTableRow">
    <td class="ewTableHeader">Nick name</td>
    <td<?php echo $chating->yahoo_id->CellAttributes() ?>><span id="cb_x_yahoo_id">
<input type="text" name="x_yahoo_id" id="x_yahoo_id"  size="30" maxlength="250" value="<?php echo $chating->yahoo_id->EditValue ?>"<?php echo $chating->yahoo_id->EditAttributes() ?>>
</span></td>
  </tr>
  <tr class="ewTableAltRow">
    <td class="ewTableHeader">Name<span class='ewmsg'>&nbsp;*</span></td>
    <td<?php echo $chating->yahoo_name->CellAttributes() ?>><span id="cb_x_yahoo_name">
<input type="text" name="x_yahoo_name" id="x_yahoo_name"  size="30" maxlength="250" value="<?php echo $chating->yahoo_name->EditValue ?>"<?php echo $chating->yahoo_name->EditAttributes() ?>>
</span></td>
  </tr>
  <tr class="ewTableRow">
    <td class="ewTableHeader">Active</td>
    <td<?php echo $chating->yahoo_active->CellAttributes() ?>><span id="cb_x_yahoo_active">
<?php
$arwrk = $chating->yahoo_active->EditValue;
if (is_array($arwrk)) {
	$armultiwrk= explode(",", strval($chating->yahoo_active->CurrentValue));
	$rowswrk = count($arwrk);
	for ($rowcntwrk = 0; $rowcntwrk < $rowswrk; $rowcntwrk++) {
		$selwrk = "";
		for ($ari = 0; $ari < count($armultiwrk); $ari++) {
			if (strval($arwrk[$rowcntwrk][0]) == trim(strval($armultiwrk[$ari]))) {
				$selwrk = " checked";
				break;
			}
		}
?>
<?php echo ew_RepeatColumnTable($rowswrk, $rowcntwrk, 5, 1) ?>
<input type="checkbox" name="x_yahoo_active[]" id="x_yahoo_active[]" value="<?php echo ew_HtmlEncode($arwrk[$rowcntwrk][0]) ?>"<?php echo $selwrk ?><?php echo $chating->yahoo_active->EditAttributes() ?>>
<?php echo $arwrk[$rowcntwrk][1] ?>
<?php echo ew_RepeatColumnTable($rowswrk, $rowcntwrk, 5, 2) ?>
<?php
	}
}
?>
</span></td>
  </tr>
</table>
<p>
<input type="submit" name="btnAction" id="btnAction" value="    Add    ">
</form>
<script language="JavaScript" type="text/javascript">
<!--

// Write your table-specific startup script here
// document.write("page loaded");
//-->

</script>
<?php include "footer.php" ?>
<?php

// If control is passed here, simply terminate the page without redirect
Page_Terminate();

// -----------------------------------------------------------------
//  Subroutine Page_Terminate
//  - called when exit page
//  - clean up connection and objects
//  - if url specified, redirect to url, otherwise end response
function Page_Terminate($url = "") {
	global $conn;

	// Page unload event, used in current page
	Page_Unload();

	// Global page unloaded event (in userfn*.php)
	Page_Unloaded();

	 // Close Connection
	$conn->Close();

	// Go to url if specified
	if ($url <> "") {
		ob_end_clean();
		header("Location: $url");
	}
	exit();
}
?>
<?php

// Load default values
function LoadDefaultValues() {
	global $chating;
}
?>
<?php

// Load form values
function LoadFormValues() {

	// Load from form
	global $objForm, $chating;
	$chating->yahoo_id->setFormValue($objForm->GetValue("x_yahoo_id"));
	$chating->yahoo_name->setFormValue($objForm->GetValue("x_yahoo_name"));
	$chating->yahoo_active->setFormValue($objForm->GetValue("x_yahoo_active"));
}

// Restore form values
function RestoreFormValues() {
	global $chating;
	$chating->yahoo_id->CurrentValue = $chating->yahoo_id->FormValue;
	$chating->yahoo_name->CurrentValue = $chating->yahoo_name->FormValue;
	$chating->yahoo_active->CurrentValue = $chating->yahoo_active->FormValue;
}
?>
<?php

// Load row based on key values
function LoadRow() {
	global $conn, $Security, $chating;
	$sFilter = $chating->SqlKeyFilter();
	if (!is_numeric($chating->no->CurrentValue)) {
		return FALSE; // Invalid key, exit
	}
	$sFilter = str_replace("@no@", ew_AdjustSql($chating->no->CurrentValue), $sFilter); // Replace key value

	// Call Row Selecting event
	$chating->Row_Selecting($sFilter);

	// Load sql based on filter
	$chating->CurrentFilter = $sFilter;
	$sSql = $chating->SQL();
	if ($rs = $conn->Execute($sSql)) {
		if ($rs->EOF) {
			$LoadRow = FALSE;
		} else {
			$LoadRow = TRUE;
			$rs->MoveFirst();
			LoadRowValues($rs); // Load row values

			// Call Row Selected event
			$chating->Row_Selected($rs);
		}
		$rs->Close();
	} else {
		$LoadRow = FALSE;
	}
	return $LoadRow;
}

// Load row values from recordset
function LoadRowValues(&$rs) {
	global $chating;
	$chating->no->setDbValue($rs->fields('no'));
	$chating->yahoo_id->setDbValue($rs->fields('yahoo_id'));
	$chating->yahoo_name->setDbValue($rs->fields('yahoo_name'));
	$chating->yahoo_active->setDbValue($rs->fields('yahoo_active'));
}
?>
<?php

// Render row values based on field settings
function RenderRow() {
	global $conn, $Security, $chating;

	// Call Row Rendering event
	$chating->Row_Rendering();

	// Common render codes for all row types
	// yahoo_id

	$chating->yahoo_id->CellCssStyle = "";
	$chating->yahoo_id->CellCssClass = "";

	// yahoo_name
	$chating->yahoo_name->CellCssStyle = "";
	$chating->yahoo_name->CellCssClass = "";

	// yahoo_active
	$chating->yahoo_active->CellCssStyle = "";
	$chating->yahoo_active->CellCssClass = "";
	if ($chating->RowType == EW_ROWTYPE_VIEW) { // View row
	} elseif ($chating->RowType == EW_ROWTYPE_ADD) { // Add row

		// yahoo_id
		$chating->yahoo_id->EditCustomAttributes = "";
		$chating->yahoo_id->EditValue = ew_HtmlEncode($chating->yahoo_id->CurrentValue);

		// yahoo_name
		$chating->yahoo_name->EditCustomAttributes = "";
		$chating->yahoo_name->EditValue = ew_HtmlEncode($chating->yahoo_name->CurrentValue);

		// yahoo_active
		$chating->yahoo_active->EditCustomAttributes = "";
		$arwrk = array();
		$arwrk[] = array("1", "Active");
		$chating->yahoo_active->EditValue = $arwrk;
	} elseif ($chating->RowType == EW_ROWTYPE_EDIT) { // Edit row
	} elseif ($chating->RowType == EW_ROWTYPE_SEARCH) { // Search row
	}

	// Call Row Rendered event
	$chating->Row_Rendered();
}
?>
<?php

// Add record
function AddRow() {
	global $conn, $Security, $chating;

	// Check for duplicate key
	$bCheckKey = TRUE;
	$sFilter = $chating->SqlKeyFilter();
	if (trim(strval($chating->no->CurrentValue)) == "") {
		$bCheckKey = FALSE;
	} else {
		$sFilter = str_replace("@no@", ew_AdjustSql($chating->no->CurrentValue), $sFilter); // Replace key value
	}
	if (!is_numeric($chating->no->CurrentValue)) {
		$bCheckKey = FALSE;
	}
	if ($bCheckKey) {
		$rsChk = $chating->LoadRs($sFilter);
		if ($rsChk && !$rsChk->EOF) {
			$_SESSION[EW_SESSION_MESSAGE] = "Duplicate value for primary key";
			$rsChk->Close();
			return FALSE;
		}
	}
	$rsnew = array();

	// Field yahoo_id
	$chating->yahoo_id->SetDbValueDef($chating->yahoo_id->CurrentValue, NULL);
	$rsnew['yahoo_id'] =& $chating->yahoo_id->DbValue;

	// Field yahoo_name
	$chating->yahoo_name->SetDbValueDef($chating->yahoo_name->CurrentValue, "");
	$rsnew['yahoo_name'] =& $chating->yahoo_name->DbValue;

	// Field yahoo_active
	$chating->yahoo_active->SetDbValueDef($chating->yahoo_active->CurrentValue, NULL);
	$rsnew['yahoo_active'] =& $chating->yahoo_active->DbValue;

	// Call Row Inserting event
	$bInsertRow = $chating->Row_Inserting($rsnew);
	if ($bInsertRow) {
		$conn->raiseErrorFn = 'ew_ErrorFn';
		$AddRow = $conn->Execute($chating->InsertSQL($rsnew));
		$conn->raiseErrorFn = '';
	} else {
		if ($chating->CancelMessage <> "") {
			$_SESSION[EW_SESSION_MESSAGE] = $chating->CancelMessage;
			$chating->CancelMessage = "";
		} else {
			$_SESSION[EW_SESSION_MESSAGE] = "Insert cancelled";
		}
		$AddRow = FALSE;
	}
	if ($AddRow) {
		$chating->no->setDbValue($conn->Insert_ID());
		$rsnew['no'] =& $chating->no->DbValue;

		// Call Row Inserted event
		$chating->Row_Inserted($rsnew);
	}
	return $AddRow;
}
?>
<?php

// Page Load event
function Page_Load() {

	//echo "Page Load";
}

// Page Unload event
function Page_Unload() {

	//echo "Page Unload";
}
?>
